#!bash

# reset first node
echo "Reset first rabbitmq node mq3"
docker exec -it rabbitmq_server_172_3 bash -c "cp /var/lib/rabbitmq/hosts /etc/hosts"
docker exec -it rabbitmq_server_172_3 bash -c "cp /var/lib/rabbitmq/hostname /etc/hostname"
# 停止服务
docker exec -it rabbitmq_server_172_3 bash -c "rabbitmqctl stop_app"
# 重置状态
docker exec -it rabbitmq_server_172_3 bash -c "rabbitmqctl reset"
# 启动服务
docker exec -it rabbitmq_server_172_3 bash -c "rabbitmqctl start_app"

# build cluster
echo "Starting to build rabbitmq cluster with two ram nodes mq2"
docker exec -it rabbitmq_server_172_2 bash -c "cp /var/lib/rabbitmq/hosts /etc/hosts"
docker exec -it rabbitmq_server_172_2 bash -c "cp /var/lib/rabbitmq/hostname /etc/hostname"
# 
docker exec -it rabbitmq_server_172_2 bash -c "rabbitmqctl stop_app"
# 重置状态
docker exec -it rabbitmq_server_172_2 bash -c "rabbitmqctl reset"
# 节点加入mq3(主节点)
docker exec -it rabbitmq_server_172_2 bash -c "rabbitmqctl join_cluster --ram rabbit@mq3"
# 启动服务
docker exec -it rabbitmq_server_172_2 bash -c "rabbitmqctl start_app"

echo "Starting to build rabbitmq cluster with two ram nodes mq4"
docker exec -it rabbitmq_server_172_4 bash -c "cp /var/lib/rabbitmq/hosts /etc/hosts"
docker exec -it rabbitmq_server_172_4 bash -c "cp /var/lib/rabbitmq/hostname /etc/hostname"
# 停止服务
docker exec -it rabbitmq_server_172_4 bash -c "rabbitmqctl stop_app"
# 重置状态
docker exec -it rabbitmq_server_172_4 bash -c "rabbitmqctl reset"
# 节点加入mq3(主节点)
docker exec -it rabbitmq_server_172_4 bash -c "rabbitmqctl join_cluster --ram rabbit@mq3"
# 启动服务
docker exec -it rabbitmq_server_172_4 bash -c "rabbitmqctl start_app"

# check cluster status
echo "Check cluster status:"
docker exec -it rabbitmq_server_172_3 bash -c "rabbitmqctl cluster_status"
docker exec -it rabbitmq_server_172_2 bash -c "rabbitmqctl cluster_status"
docker exec -it rabbitmq_server_172_4 bash -c "rabbitmqctl cluster_status"
